package com.springchang.leetcode.array;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * leetcode 1号题目：两数之和
 */
public class TwoSum_1 {

    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            //计算还需要多少才能得到和
            int other = target - nums[i];

            //看看other数据是否再map集合里面
            if(map.get(other) != null) {
                int[] arr = new int[2];
                arr[0] = i;
                arr[1] = map.get(other);
                return arr;
            } else {
                map.put(nums[i], i);
            }
        }
        return null;
    }

    public static void main(String[] args) {
        int[] nums = new int[] {2, 7, 11, 15};
        int target = 9;

        TwoSum_1 demo = new TwoSum_1();
        System.out.println(String.format("结果: %s", Arrays.toString(demo.twoSum(nums, target))));
    }
}

